package com.ruoyi.wetest.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wetest.utils.JsonHelper;
import com.ruoyi.wetest.vo.WeTestReportSummarizationVo;
import com.ruoyi.wetest.manager.WetestReportManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Allen
 * @description TODO
 * @Date 2024/5/10 17:22
 */
@RestController
@RequestMapping("/wetest/report/")
public class WetestRecordReportController extends BaseController {

    @Autowired
    private WetestReportManager wetestReportManager;

    @Autowired
    private ResourceLoader resourceLoader;


    /**
     * 查看压测报告概述
     * @param testId
     * @return
     */
    @GetMapping("/summarization/{testId}")
    public AjaxResult reportSummarization(@PathVariable("testId") String testId) {
        WeTestReportSummarizationVo vo = wetestReportManager.getReportSummarization(testId);
        if (vo==null){
            return error("报告不存在");
        }
        return success(vo);
    }



    /**
     * 查看压测报告所有的事务列表
     * @param testId
     * @return
     */
    @GetMapping("/trans_list/{testId}")
    @Deprecated(since = "废弃")
    public AjaxResult reportTransList(@PathVariable("testId") String testId) {
        return success(wetestReportManager.selectWetestStatLogTypeList(testId));
    }

    /**
     * 查看压测某个事务的数据记录
     * @param testId
     * @return
     */
    @GetMapping("/stat_list/{testId}/{trans}")
    @Deprecated(since = "废弃")
    public AjaxResult reportStatList(@PathVariable("testId") String testId,@PathVariable("trans") String trans) {
        List<Object[]> list = wetestReportManager.getStatListByType(testId, trans);
        return success(list);
    }

    /**
     * 查看压测某个事务的数据信息
     * @param testId
     * @return
     */
    @GetMapping("/stat_info/{testId}/{trans}")
    public AjaxResult reportStatInfo(@PathVariable("testId") String testId,@PathVariable("trans") String trans) {
        return success(wetestReportManager.getStatInfo(testId, trans));
    }


    /**
     * 获取压测报告规则
     * @param type
     * @return
     */
    @GetMapping("/rule_info/{type}")
    public AjaxResult reportRuleInfo(@PathVariable("type") int type) {
        JSONObject o = JsonHelper.jsonFileLoad(resourceLoader, "StressModuleConfig.json");
        Map<String,Integer> m = new HashMap<>();
        for (String s : o.keySet()) {
            JSONObject o1 = o.getJSONObject(s);
            String name = o1.getString("name");
            Integer tps = o1.getInteger("tps");
            m.put(name,tps);
        }
        return success(m);
    }

}
